package com.such.kit.database.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.such.kit.validate.ValidateCommons;

/**
 * @作者 SUCH
 * @日期 2017-6-9 下午6:08:25
 * @描述 数据分页类
 * <p>
 * 分页取值下标默认从 1 开始：<br>
 * 例：<br>
 * pageLimit = 10<br>
 * pageIndex = 1 > [startRow = 1 | endRow = 10 | startRowSubscript0 = 0 | endRowSubscript0 = 9]<br>
 * pageIndex = 2 > [startRow = 11 | endRow = 20 | startRowSubscript0 = 10 | endRowSubscript0 = 19]<br>
 * pageIndex = 3 > [startRow = 21 | endRow = 30 | startRowSubscript0 = 20 | endRowSubscript0 = 29]<br>
 * </p>
 */
public class Page<T> implements Serializable {

	private static final long serialVersionUID = 6305437563794954425L;
	private int pageIndex;
	private int pageLimit;
	private int pageTotal;
	private int rowTotal;
	/** startRow：开始行，默认下标从 1 开始 */
	private int startRow;
	/** startRowSubscript0：开始行，下标从 0 开始 */
	private int startRowSubscript0;
	/** endRow：结束行，默认下标从 1 开始 */
	private int endRow;
	/** endRowSubscript0：结束行，下标从 0 开始 */
	private int endRowSubscript0;
	private boolean isFirst;
	private boolean isLast;
	private List<T> results;

	/**
	 * <p>构造函数</p>
	 * @param pageIndex 页数
	 * @param pageLimit 每页数据条数
	 */
	public Page(int pageIndex, int pageLimit) {
		this(pageIndex, pageLimit, pageIndex * pageLimit, null);
	}

	/**
	 * <p>构造函数</p>
	 * @param pageIndex	页数，默认值为 1
	 * @param pageLimit 每页数据条数，默认值为 10
	 * @param rowTotal 数据总量
	 * @param results 结果集
	 */
	public Page(int pageIndex, int pageLimit, int rowTotal, List<T> results) {
		if (pageIndex == 0) {
			pageIndex = 1;
		}
		if (pageLimit == 0) {
			pageLimit = 10;
		}
		int pageTotal = 0;
		int startRow = 1;
		int startRowSubscript0 = 0;
		int endRow = 1;
		int endRowSubscript0 = 0;
		boolean isFirst = false;
		boolean isLast = false;
		if (rowTotal == 0) {
			pageIndex = 0;
			results = new ArrayList<T>();
			isFirst = true;
			isLast = true;
		} else {
			if (rowTotal % pageLimit == 0) {
				pageTotal = rowTotal / pageLimit;
			} else {
				pageTotal = rowTotal / pageLimit + 1;
			}
			if (pageIndex == 1) {
				isFirst = true;
			}
			if (pageIndex >= pageTotal) {
				isLast = true;
			}
			startRowSubscript0 = (pageIndex - 1) * pageLimit;
			startRow = startRowSubscript0 + 1;
			if (isLast) {
				endRowSubscript0 = rowTotal - 1;
			} else {
				endRowSubscript0 = startRowSubscript0 + pageLimit - 1;
			}
			endRow = endRowSubscript0 + 1;
		}
		setPageIndex(pageIndex);
		setPageLimit(pageLimit);
		setPageTotal(pageTotal);
		setRowTotal(rowTotal);
		setStartRow(startRow);
		setStartRowSubscript0(startRowSubscript0);
		setEndRow(endRow);
		setEndRowSubscript0(endRowSubscript0);
		setResults(results);
		this.isFirst = isFirst;
		this.isLast = isLast;
	}

	public int getPageIndex() {
		return this.pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageLimit() {
		return this.pageLimit;
	}

	public void setPageLimit(int pageLimit) {
		this.pageLimit = pageLimit;
	}

	public int getPageTotal() {
		return this.pageTotal;
	}

	public void setPageTotal(int pageTotal) {
		this.pageTotal = pageTotal;
	}

	public int getRowTotal() {
		return this.rowTotal;
	}

	public void setRowTotal(int rowTotal) {
		this.rowTotal = rowTotal;
	}

	public int getStartRow() {
		return this.startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	public int getStartRowSubscript0() {
		return this.startRowSubscript0;
	}

	public void setStartRowSubscript0(int startRowSubscript0) {
		this.startRowSubscript0 = startRowSubscript0;
	}

	public int getEndRow() {
		return this.endRow;
	}

	public void setEndRow(int endRow) {
		this.endRow = endRow;
	}

	public int getEndRowSubscript0() {
		return this.endRowSubscript0;
	}

	public void setEndRowSubscript0(int endRowSubscript0) {
		this.endRowSubscript0 = endRowSubscript0;
	}

	public List<T> getResults() {
		return this.results;
	}

	public void setResults(List<T> results) {
		if (ValidateCommons.isEmpty(results)) {
			results = new ArrayList<T>();
		}
		this.results = results;
	}

	public boolean isFirst() {
		return this.isFirst;
	}

	public boolean isLast() {
		return this.isLast;
	}

}
